package com.linln.wxapi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.linln.common.enums.TypeEnum;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.MemberCollect;
import com.linln.modules.member.domain.Membernotice;
import com.linln.modules.member.service.MemberCollectService;
import com.linln.modules.member.service.MembernoticeService;
import com.linln.modules.message.domain.MessageInfo;
import com.linln.modules.message.service.MessageInfoService;
import com.linln.modules.modules.reply.domain.Reply;
import com.linln.modules.modules.reply.service.ReplyService;
import com.linln.modules.store.domain.Storeinfo;
import com.linln.modules.store.service.StoreinfoService;
import com.linln.modules.tribe.domain.Tribe;
import com.linln.modules.tribe.service.TribeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商户相关小程序接口
 * @author hct
 *
 */
@Api(value="收藏管理")
@RestController
@RequestMapping("/wx/collect/{area}")
public class WxApiCollectController {
	
	 @Autowired
	  private MemberCollectService memberCollectService;
	 
	 @Autowired
	  private MessageInfoService messageService;
	 @Autowired
	  private StoreinfoService storeService;
	 
	 @Autowired
	 private ReplyService replyService;
	 
	 @Autowired
	 private TribeService tribeService;

	  @Autowired
	  private MembernoticeService noticeService;

	/**
	 * 消息收藏列表
	 * @param area 区域
	 * @param lng  经度
	 * @param lat  纬度
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "收藏信息数量",  notes = "收藏信息数量")
	@RequestMapping(value = "collectcount",method= {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ResultVo myCollectCount(@PathVariable String area,Long memberId) {
		Long count=memberCollectService.countCollect(memberId);
		Map map=new HashMap();
        map.put("count",count);
		return ResultVoUtil.success(map);

	}
		/**
         * 消息数量
         * @param area 区域
         * @param lng  经度
         * @param lat  纬度
         * @return
         */
		@ApiOperation(value = "收藏信息",  notes = "收藏信息")
	    @RequestMapping(value = "collectlist",method= {RequestMethod.POST,RequestMethod.GET})
	    @ResponseBody
	    public ResultVo messagecollectlist(@PathVariable String area,Long memberId,Integer type) {
	       
	        	 try {
	              	// 创建匹配器，进行动态查询匹配
	                  ExampleMatcher matcher = ExampleMatcher.matching()
	                		  .withMatcher("memberId", match -> match.equals(memberId))
	                          .withMatcher("area", match -> match.equals(area));
	                 MemberCollect collect=new MemberCollect();
	                  collect.setType(type); 
	                  collect.setArea(area); 
                      collect.setMemberId(memberId); 
	                  // 获取数据列表
	                  Example<MemberCollect> example = Example.of(collect, matcher);
	                  Page<MemberCollect> list = memberCollectService.getPageList(example);
	                  Map map=new HashMap();
		              if(list!=null && list.getTotalElements()>0) {
	                	  List<MemberCollect> collectlist=list.getContent();
	                	  List<Long> ids=new ArrayList<Long>();
	                	  for(int i=0;i<collectlist.size();i++) {
	                		  ids.add(collectlist.get(i).getTypeId());
	                	  }
	                	  if(type==TypeEnum.MESSAGE.getCode()) {
	                		  //
	                		  List<MessageInfo> messageList=messageService.getList( ids);
	    	                  map.put("list", messageList);

 	                	  }else if(type==TypeEnum.STORE.getCode()){
	                		  //
	                		 List<Storeinfo> storelist= storeService.getList( ids);
	    	                  map.put("list", storelist);

	                	  }else if(type==TypeEnum.REPLY.getCode()){
	                		  //
	                		  List<Reply> replylist=replyService.getList( ids);
	    	                  map.put("list", replylist);

	                	  }
	                	  else if(type==TypeEnum.TRIBE.getCode()){
	                		  //
	                		  List<Tribe> tribelist=tribeService.getList( ids);
	    	                  map.put("list", tribelist);

	                	  }
	                  }
	                  map.put("pageNumber", list.getPageable().getPageNumber()+1);
	                  map.put("pageSize", list.getPageable().getPageSize());
	                  	
	        	 return ResultVoUtil.success(map);
	        } catch (Exception e) {
	        	return ResultVoUtil.error("失败"); 
	        }
	 
	        }
	    
	    
	/**
	* 消息收藏列表
	* @param area 区域
	* @param lng  经度
	* @param lat  纬度
	* @return
	*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "消息信息列表",  notes = "消息信息列表")
	@RequestMapping(value = "noticelist",method= {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ResultVo noticelist(@PathVariable String area,Long memberId) {

	 	 try {
	       	// 创建匹配器，进行动态查询匹配
	           ExampleMatcher matcher = ExampleMatcher.matching()
	         		  .withMatcher("memberId", match -> match.equals(memberId))
	                   .withMatcher("area", match -> match.equals(area));
	           Membernotice notice=new Membernotice();
	           //type.setPid(0l);
	           // 获取数据列表
	           Example<Membernotice> example = Example.of(notice, matcher);
	           Page<Membernotice> list = noticeService.getPageList(example);
	           Map map=new HashMap();
	           map.put("list", list.getContent());
	           map.put("pageNumber", list.getPageable().getPageNumber()+1);
	           map.put("pageSize", list.getPageable().getPageSize());
	           	
	 	 return ResultVoUtil.success(map);
	 } catch (Exception e) {
	 	return ResultVoUtil.error("失败"); 
	 }

	 }
	
}

    

